//
//  SelectDepositViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15/9/27.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "SelectDepositViewController.h"
#import "SelectDepositTableViewCell.h"
#import "AddNewDepositViewController.h"
#import "CashierViewController.h"
@interface SelectDepositViewController ()<AddNewDepositDelegate,CashierViewControllerDelegate
>
{
    NSMutableArray *numArray;
    NSMutableArray *dateArray;
    NSMutableArray *goodInfoArray;
    NSMutableArray *priceSopeArray;
    NSMutableArray *remarkArray;
    NSMutableArray *priceArray;
    NSMutableArray *depositDocEntryArray;
   // NSMutableArray *selectRowIDArray;
    NSUInteger _PageIndex;
    NSUInteger _TotalCount;
    NSUInteger _TotalPage;
    
    NSString *imageStr;
    
   // NSUInteger cellTag;
    
    AddNewDepositViewController *addNewDepositVC;
    CashierViewController *cashierVC;
    
    
}
@end

@implementation SelectDepositViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // Do any additional setup after loading the view from its nib.
    [_alphaViewBtn setBackgroundColor:[Global parseColor:WESTORE_TRANSCLUCENT]];
    [_alphaViewBtn addTarget:self action:@selector(alphaViewClick) forControlEvents:UIControlEventTouchUpInside];
    [_addNewDepositBtn addTarget:self action:@selector(addNewDepositBtnClick) forControlEvents:UIControlEventTouchUpInside];
    depositDocEntryArray=[NSMutableArray array];
    numArray=[NSMutableArray array];
    dateArray=[NSMutableArray array];
    goodInfoArray=[NSMutableArray array];
    priceSopeArray=[NSMutableArray array];
    remarkArray=[NSMutableArray array];
    priceArray=[NSMutableArray array];
   // selectRowIDArray=[NSMutableArray array];
    _PageIndex=0;
    [self loadDepositNextPage:_PageIndex];
    

}
-(void)addNewDepositBtnClick
{
    if (addNewDepositVC.view!=nil) {
        
        [addNewDepositVC removeFromParentViewController];
        [addNewDepositVC.view removeFromSuperview];
        
    }
    addNewDepositVC=[[AddNewDepositViewController alloc] init];
    addNewDepositVC.memberAndPhoneStr=_memberAndPhoneStr;
    addNewDepositVC.gradeStr=_gradeStr;
    addNewDepositVC.integralStr=_integralStr;
    addNewDepositVC.memberCodeStr=_memberCodeStr;
    addNewDepositVC.delegate=self;
    // selectDepositVC.memberCodeStr=memberCodeStr;
    addNewDepositVC.view.frame = CGRectMake(0,
                                            0,
                                            self.view.bounds.size.width,
                                            self.view.bounds.size.height);
    
    [self addChildViewController:addNewDepositVC];
    [self.view addSubview:addNewDepositVC.view];
//    if ([self.delegate respondsToSelector:@selector(addNewDepositButtonClick)]) {
//        [self.delegate addNewDepositButtonClick];
//    }

}
//addNewDepositDelegate
-(void)ConfirmAddDepositClick:(NSString *)codeStr
{
    if (addNewDepositVC.view!=nil) {

        [addNewDepositVC removeFromParentViewController];
        [addNewDepositVC.view removeFromSuperview];

    }
//    [depositDocEntryArray removeAllObjects];
//    [numArray removeAllObjects];
//    [dateArray removeAllObjects];
//    [goodInfoArray removeAllObjects];
//    [priceSopeArray removeAllObjects];
//    [remarkArray removeAllObjects];
//    [priceArray removeAllObjects];
//    // selectRowIDArray=[NSMutableArray array];
//    _PageIndex=0;
//    [self loadDepositNextPage:_PageIndex];
    if (cashierVC.view!=nil) {
        
        [cashierVC removeFromParentViewController];
        [cashierVC.view removeFromSuperview];
        
    }
    cashierVC=[[CashierViewController alloc] init];
    cashierVC.delegate=self;
    cashierVC.docTypeStr=@"Deposit";
    cashierVC.docNumStr=codeStr;
    cashierVC.view.frame = CGRectMake(0,
                                      0,
                                      self.view.bounds.size.width,
                                      self.view.bounds.size.height);
    
    [self addChildViewController:cashierVC];
    [self.view addSubview:cashierVC.view];
    //[self.view bringSubviewToFront:cashierVC.view];

}
//cashierViewcontrollerDelegate
-(void)CashierAlphaViewClick
{
    if (cashierVC.view!=nil) {
        
        [cashierVC removeFromParentViewController];
        [cashierVC.view removeFromSuperview];
        
    }
    
}
-(void)CashierSuccess
{
    if (cashierVC.view!=nil) {
        
        [cashierVC removeFromParentViewController];
        [cashierVC.view removeFromSuperview];
        
    }
        [depositDocEntryArray removeAllObjects];
        [numArray removeAllObjects];
        [dateArray removeAllObjects];
        [goodInfoArray removeAllObjects];
        [priceSopeArray removeAllObjects];
        [remarkArray removeAllObjects];
        [priceArray removeAllObjects];
        // selectRowIDArray=[NSMutableArray array];
        _PageIndex=0;
        [self loadDepositNextPage:_PageIndex];
}

-(void)AddNewDepositAlphaViewClick
{
    if (addNewDepositVC.view!=nil) {
        
        [addNewDepositVC removeFromParentViewController];
        [addNewDepositVC.view removeFromSuperview];
        
    }
    
}

-(void) loadDepositNextPage:(NSUInteger)oldPageNum {
    if(oldPageNum!=0 && _TotalCount<=numArray.count)
        return;
    if(oldPageNum == 0){
        [numArray removeAllObjects];
        [_depositTableView reloadData];
    }
    _PageIndex=oldPageNum+1;
    [self requestOrderList: _PageIndex pageSize:7];
}
-(void)requestOrderList:(NSUInteger)pageIndex pageSize:(NSUInteger)pageSize
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        NSNumber *pageSizeNumber = [NSNumber numberWithInteger:pageSize];
        NSNumber *pageIndexNumber = [NSNumber numberWithInteger:pageIndex];
        
        [params setObject:pageSizeNumber forKey:@"PageSize"];
        [params setObject:pageIndexNumber forKey:@"PageIndex"];
        [params setObject:_memberCodeStr forKey:@"MemberCode"];
        
       // NSLog(@"ssssssssss=%@",params);
        
        [HttpRequest requestData:@"Guide/GetDepositList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                  //  NSLog(@"severdata===%@",severDataDict.dataDict);
                    [Global hideProgressHUDInternal:self.view];
                   
                    if (_TotalCount%7!=0) {
                        _TotalPage=_TotalCount/7+1;
                    }
                    else if (_TotalCount%7==0)
                    {
                        _TotalPage=_TotalCount/7;
                    }
                    
                    
                NSMutableArray *mDepositArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                   
                    if ( mDepositArray.count>0) {
                        for (int i=0; i< mDepositArray.count; i++) {
                            
                            [depositDocEntryArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"DocEntry"]];
                            [numArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"DocNum"]];
                            [dateArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"DocDate"]];
                            [goodInfoArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"ItemName"]];
                            NSString *maxStr=[[mDepositArray objectAtIndex:i] objectForKey:@"MaxAmount"];
                            NSString *minStr=[[mDepositArray objectAtIndex:i] objectForKey:@"MinAmount"];
                            NSString *scopeStr=[NSString stringWithFormat:@"%@~%@",minStr,maxStr];
                            [priceSopeArray addObject:scopeStr];
                            [priceArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"Subscription"]];
                            [remarkArray addObject:[[mDepositArray objectAtIndex:i] objectForKey:@"Remark"]];
                           
                        }
                    }
                    [_depositTableView reloadData];
                    if (_cellTag.length>0) {
                        
                    
                    NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:[_cellTag integerValue] inSection:0];
                    UITableViewCell *cell = [_depositTableView cellForRowAtIndexPath:indexPath1];
                    SelectDepositTableViewCell*depositCell=( SelectDepositTableViewCell *)cell;
                    // [depositCell.statusImage setImage:[UIImage imageNamed:@"xuanze_normal.png"]];
                    //if (i==indexPath.row) {
                    
                    [depositCell.statusImage setImage:[UIImage imageNamed:@"xuanze_hight1.png"]];
                    
                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    [Global loginViewController:self noteStr:requestResultStr];
                        // [Global loginViewController:self];
                    }
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return numArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 135;
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * const  SelectDepositIdentifier = @"SelectDepositIdentifier";
    SelectDepositTableViewCell *cell=( SelectDepositTableViewCell *)[tableView dequeueReusableCellWithIdentifier: SelectDepositIdentifier];
    if (cell==nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SelectDepositTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
      //  cell.delegate=self;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if (numArray.count>0) {
        cell.numLabel.text=[numArray objectAtIndex:indexPath.row];
     //   [self resizeLabel:cell.numLabel];
        [Global resizeForLable:cell.numLabel WithText:cell.numLabel.text parentView:self.view];
        CGRect frame=cell.dateLabel.frame;
        frame.origin.x=cell.numLabel.frame.origin.x+cell.numLabel.frame.size.width+30;
        cell.dateLabel.frame=frame;
    }
    
    if (dateArray.count>0) {
        cell.dateLabel.text=[dateArray objectAtIndex:indexPath.row];
        [Global resizeForLable:cell.dateLabel WithText:cell.dateLabel.text parentView:self.view];
    }
    if (goodInfoArray.count>0) {
        cell.goodInfoLabel.text=[goodInfoArray objectAtIndex:indexPath.row];
    }
    if (remarkArray.count>0) {
        cell.remarkLabel.text=[remarkArray objectAtIndex:indexPath.row];
    }
    if (priceSopeArray.count>0) {
        cell.priceScopeLabel.text=[priceSopeArray objectAtIndex:indexPath.row];
    }
    if (priceArray.count>0) {
        float AvgPrice=[[priceArray objectAtIndex:indexPath.row] floatValue];
        NSString *AvgPriceStr=[Global floatConvertPercentile:AvgPrice];
        cell.priceLabel.text=[NSString stringWithFormat:@"¥%@",AvgPriceStr];
    }
    
    if (indexPath.row == [numArray count] - 1)
    {
        if (_PageIndex<=_TotalPage) {
            //  NSLog(@"###LOADING???");
            [self loadDepositNextPage:_PageIndex];
        }
    }
    return cell;



}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cellTag=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
  // for (int i=0; i<numArray.count; i++) {
        
    
//    NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:indexPath.row  inSection:0];
//    UITableViewCell *cell = [_depositTableView cellForRowAtIndexPath:indexPath1];
//    SelectDepositTableViewCell*depositCell=( SelectDepositTableViewCell *)cell;
//  // [depositCell.statusImage setImage:[UIImage imageNamed:@"xuanze_normal.png"]];
//        //if (i==indexPath.row) {
//            
//    [depositCell.statusImage setImage:[UIImage imageNamed:@"xuanze_hight1.png"]];
      //  }
    //}
    
    float AvgPrice=[[priceArray objectAtIndex:indexPath.row] floatValue];
    NSString *AvgPriceStr=[NSString stringWithFormat:@"%0.02f",AvgPrice];
    
    NSString *entryString=[NSString stringWithFormat:@"%ld",(long)[[depositDocEntryArray objectAtIndex:indexPath.row] integerValue]];
    
    
    if ([self.delegate respondsToSelector:@selector(depositConfirmSelectBtnClick:depositAmoutStr:cellTagStr:)]) {
        [self.delegate depositConfirmSelectBtnClick:entryString depositAmoutStr:AvgPriceStr cellTagStr:_cellTag];
    }

    
}
-(void)alphaViewClick
{
    if ([self.delegate respondsToSelector:@selector(selectDepositAlphaViewClick)]) {
        [self.delegate selectDepositAlphaViewClick];
    }
}
-(IBAction)confirmClick:(id)sender
{
   
//    [numArray removeAllObjects];
//    [dateArray removeAllObjects];
//    [goodInfoArray removeAllObjects];
//    [priceSopeArray removeAllObjects];
//    [remarkArray removeAllObjects];
//    [priceArray removeAllObjects];
//    [depositDocEntryArray removeAllObjects];
    
//    float AvgPrice=[[priceArray objectAtIndex:cellTag] floatValue];
//    NSString *AvgPriceStr=[NSString stringWithFormat:@"%0.02f",AvgPrice];
//    
//    NSString *entryString=[NSString stringWithFormat:@"%ld",(long)[[depositDocEntryArray objectAtIndex:cellTag] integerValue]];
//    
//    
//    if ([self.delegate respondsToSelector:@selector(depositConfirmSelectBtnClick:depositAmoutStr:)]) {
//        [self.delegate depositConfirmSelectBtnClick:entryString depositAmoutStr:AvgPriceStr];
//    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
